package dao

import (
	"fmt"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/scanlogs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// -------------------- dao层 - 扫码日志相关操作 -----------------------//

type ScanLogsDao struct {
}

func NewScanLogsDao() *ScanLogsDao {
	return &ScanLogsDao{}
}

// InsertScanLogs 新增扫码日志
func (d *ScanLogsDao) InsertScanLogs(m *scanlogs.ScanLogsModel) *dingo.ErrorWrap {

	db := global.Orm.Table("scan_logs").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

// GetLoginLogsList            登录日志查询列表
func (d *ScanLogsDao) GetScanLogsList(param *request.ScanLogsListReq) *dingo.ErrorWrap {
	var (
		lst    []scanlogs.ScanLogsModel
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)
	db := global.Orm.Table("scan_logs")
	if param.PlaceName != "" { db.Where("place_name like ?", fmt.Sprint("%", param.PlaceName, "%")) }
	if param.MonthStr != "" {
		db.Where("DATE_FORMAT(FROM_UNIXTIME(create_at),'%Y-%m') = ?", param.MonthStr)
	}
	if param.DayStr != "" {
		db.Where("DATE_FORMAT(FROM_UNIXTIME(create_at),'%Y-%m-%d') = ?", param.DayStr)
	}
	db.Order("id desc")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}
